package com.baboba.rummy.web.client;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.baboba.rummy.cards.Card;
import com.baboba.rummy.cards.CardBitMap;
import com.baboba.rummy.cards.CardComparator;
import com.baboba.rummy.cards.CardFactory;
import com.baboba.rummy.cards.ColorCard;
import com.baboba.rummy.game.IceBreaker;
import com.baboba.rummy.web.client.card.CardListView;
import com.baboba.rummy.web.client.card.IceBreakView;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class IceBreak implements EntryPoint {

	private Button randomButton = new Button("Random");
	private FlexTable cardsFlexTable = new FlexTable();
	private IceBreakView iceBreakView = new IceBreakView();
	
	@Override
	public void onModuleLoad() {
		randomButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				LinkedList<Card> allCards = CardFactory.generateRandomCardsAsList();
				final List<Card> currentCards = new  LinkedList<Card>();
				
				for (int i=1;i<=14;i++){
					currentCards.add(allCards.getFirst());
					allCards.removeFirst();
				}
				
				renderCardsTables(currentCards);
			}

			
			
		});
		
		List<LinkedList<Card>> cards = getTestCards();
		for (LinkedList<Card> cardList:cards)
			renderCardsTables(cardList);
		
		VerticalPanel topContainer = new VerticalPanel();
		topContainer.add(randomButton);
		topContainer.add(iceBreakView);
		topContainer.add(cardsFlexTable);
		RootLayoutPanel.get().add(topContainer);
	}
	
	private void renderCardsTables(final List<Card> currentCards) {
		Collections.sort(currentCards, new CardComparator());
		CardListView cardListView = new CardListView(currentCards);
		
		cardsFlexTable.insertRow(0);
		cardsFlexTable.setWidget(0, 0, cardListView);
		
		Button button = new Button("Ice Break");
		button.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				iceBreakView.showIceBreak(IceBreaker.awkwardBreak(new CardBitMap(currentCards)));
			}
			
		});
		
		cardsFlexTable.setWidget(0, 1, button);
	}
	
	public List<LinkedList<Card>> getTestCards(){
		List<LinkedList<Card>> result = new LinkedList<LinkedList<Card>>();
		
		LinkedList<Card> cards = new LinkedList<Card>();
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Black));
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Blue));
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Blue));
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(1, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(3, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(5, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(7, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(9, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(11, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(2, com.baboba.rummy.cards.Color.Red));
		result.add(cards);
		
		cards = new LinkedList<Card>();
		cards.add(new ColorCard(13, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(12, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(11, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(10, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(9, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(1, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(3, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(5, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(7, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(9, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(11, com.baboba.rummy.cards.Color.Orange));
		cards.add(new ColorCard(2, com.baboba.rummy.cards.Color.Red));
		cards.add(new ColorCard(2, com.baboba.rummy.cards.Color.Black));
		cards.add(new ColorCard(2, com.baboba.rummy.cards.Color.Blue));
		
		
		result.add(cards);
		
		return result;
	}
}
